package com.zmn.brs.services.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.zmn.brs.common.constant.BrsMqConst;
import com.zmn.brs.common.model.mq.OperateRecordMq;
import com.zmn.brs.services.interfaces.IOperateRecordService;
import com.zmn.manager.mq.interfaces.ZmnMQSender;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * 类描述：操作记录接口实现
 *
 * @author zhangyao
 * @since 2021/04/22 9:59
 */
@Slf4j
public class IOperateRecordServiceImpl implements IOperateRecordService {

    @NacosValue(value = "${spring.mq.topic.brs}", autoRefreshed = true)
    String brsTopic;

    private static final String OBJECT_ID = "0";

    @Resource
    private ZmnMQSender zmnMqSender;

    @Override
    public void record(OperateRecordMq recordMq) {

        if (StringUtils.isEmpty(recordMq.getObjectId()) || OBJECT_ID.equals(recordMq.getObjectId())) {
            log.error("【安全监控】操作记录，发送MQ消息失败，对象Id为空或者为0，消息内容：{}", recordMq);
            return;
        }
        try {
            // 对象ID为系统，不进行安全监控
            if (OperateRecordMq.getObjectIdIsSystem(recordMq.getObjectId())) {
                log.error("【安全监控】操作记录，发送MQ消息失败，对象ID为系统，消息内容：{}", recordMq);
                return;
            }
            // 对象身份不在监控范围内，不进行安全监控
            if (!OperateRecordMq.getObjectIdentityIsMonitoringScope(recordMq.getObjectIdentity())) {
                log.error("【安全监控】操作记录，发送MQ消息失败，对象身份不在监控范围内，消息内容：{}", recordMq);
                return;
            }
            zmnMqSender.send(brsTopic, BrsMqConst.OPERATE_RECORD_MESSAGE, UUID.randomUUID().toString(), JSON.toJSONString(recordMq));
            log.info("【安全监控】操作记录，发送MQ消息内容：{}", recordMq);
        } catch (Exception e) {
            log.info("【安全监控】操作记录，发送MQ消息失败，异常信息：{}", e);
        }
    }

}
